package com.example.myapplication.srt;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.regex.Pattern;

public class SubtitlesDataCoding {
    /**
     * 一秒=1000毫秒
     */
    private final int oneSecond = 1000;

    private final int oneMinute = 60 * oneSecond;

    private final int oneHour = 60 * oneMinute;

    /**
     * 每一个数据节点
     */
    public ArrayList<SubtitlesModel> list = new ArrayList<SubtitlesModel>();

    /**
     * 正则表达式，判断是否是时间的格式
     */
    private final String equalStringExpress = "\\d\\d:\\d\\d:\\d\\d,\\d\\d\\d --> \\d\\d:\\d\\d:\\d\\d,\\d\\d\\d";

    /**
     * 读取本地文件
     *
     * @param path
     */
    public ArrayList<SubtitlesModel> readFile(String path, boolean en) {
        String line;
        FileInputStream is;
        File subtitlesFile = new File(path);
        BufferedReader in = null;

        if (!subtitlesFile.exists() || !subtitlesFile.isFile()) {
            Log.e("jixiongxu:", "open subtitle file fill");
            return null;
        }
        /**
         * 读取文件，转流，方便读取行
         */
        try {
            is = new FileInputStream(subtitlesFile);
            try {
                in = new BufferedReader(new InputStreamReader(is, "gbk"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            assert in != null;
            while ((line = in.readLine()) != null) {
                SubtitlesModel sm = new SubtitlesModel();
                if( path.contains("01 Work and Play") && list.size()  ==73){
                    System.out.println("");
                }
                // 匹配正则表达式，不符合提前结束当前行；
                if (Pattern.matches(equalStringExpress, line)) {
                    // 填充开始时间数据
                    sm.star = getTime(line.substring(0, 12));
                    // 填充结束时间数据
                    sm.end = getTime(line.substring(17, 29));
                    String trans;
                    StringBuilder stringBuilder = new StringBuilder();
                    while ((trans = in.readLine()) != null && !trans.equals("")) {
                        trans = trans.replace("木乃伊","妈妈");
                        stringBuilder.append(trans+" ");
                    }
                    if (en) {
                        sm.contextE = stringBuilder.toString();
                    } else {
                        sm.contextC = stringBuilder.toString();
                    }

//                    // 填充中文数据
//                    sm.contextC = in.readLine();
//                    // 填充英文数据
//                    sm.contextE = in.readLine();
                    // 当前字幕的节点位置
                    sm.node = list.size() + 1;
                    list.add(sm);
                }
            }
            return getSubtitles();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        if (list != null) {
            Log.d("jixiongxu:", "open subtitle file ok");
            return getSubtitles();
        }
        return null;
    }

    /**
     * @param line
     * @return 字幕所在的时间节点
     * @descraption 将String类型的时间转换成int的时间类型
     */
    private int getTime(String line) {
        try {
            return Integer.parseInt(line.substring(0, 2)) * oneHour// 时
                    + Integer.parseInt(line.substring(3, 5)) * oneMinute// 分
                    + Integer.parseInt(line.substring(6, 8)) * oneSecond// 秒
                    + Integer.parseInt(line.substring(9, line.length()));// 毫秒
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * @return list?null
     * @descraption 返回解析后的字幕数据
     */
    public ArrayList<SubtitlesModel> getSubtitles() {
        return list != null && list.size() > 0 ? list : null;
    }
}
